/**
 * 环境配置管理
 * 统一管理所有环境相关的配置
 */

// API配置
export const API_CONFIG = {
  BASE_URL: process.env.REACT_APP_API_URL || '/api',
  TIMEOUT: Number(process.env.REACT_APP_API_TIMEOUT) || 10000,
  RETRY_TIMES: Number(process.env.REACT_APP_API_RETRY) || 3,
} as const;

// 文件上传配置
export const UPLOAD_CONFIG = {
  MAX_FILE_SIZE: Number(process.env.REACT_APP_UPLOAD_MAX_SIZE) || 10 * 1024 * 1024, // 10MB
  ALLOWED_IMAGE_TYPES: ['.jpg', '.jpeg', '.png', '.gif', '.webp'],
  ALLOWED_TORRENT_TYPES: ['.torrent'],
  CHUNK_SIZE: Number(process.env.REACT_APP_UPLOAD_CHUNK_SIZE) || 1024 * 1024, // 1MB
} as const;

// 资源URL配置
export const RESOURCE_CONFIG = {
  AVATAR_BASE_URL: process.env.REACT_APP_AVATAR_URL || 'http://localhost:8080',
  STATIC_BASE_URL: process.env.REACT_APP_STATIC_URL || 'http://localhost:8080',
  CDN_BASE_URL: process.env.REACT_APP_CDN_URL || '',
} as const;

// 应用配置
export const APP_CONFIG = {
  NAME: process.env.REACT_APP_NAME || 'PT站点',
  VERSION: process.env.REACT_APP_VERSION || '1.0.0',
  ENVIRONMENT: process.env.NODE_ENV || 'development',
  DEBUG: process.env.NODE_ENV === 'development',
} as const;

// 功能开关配置
export const FEATURE_FLAGS = {
  ENABLE_REGISTRATION: process.env.REACT_APP_ENABLE_REGISTRATION !== 'false',
  ENABLE_FORUM: process.env.REACT_APP_ENABLE_FORUM !== 'false',
  ENABLE_MESSAGES: process.env.REACT_APP_ENABLE_MESSAGES !== 'false',
  ENABLE_REQUESTS: process.env.REACT_APP_ENABLE_REQUESTS !== 'false',
  ENABLE_ANALYTICS: process.env.REACT_APP_ENABLE_ANALYTICS === 'true',
} as const;

// 缓存配置
export const CACHE_CONFIG = {
  USER_INFO_TTL: Number(process.env.REACT_APP_CACHE_USER_TTL) || 5 * 60 * 1000, // 5分钟
  TORRENT_LIST_TTL: Number(process.env.REACT_APP_CACHE_TORRENT_TTL) || 2 * 60 * 1000, // 2分钟
  FORUM_LIST_TTL: Number(process.env.REACT_APP_CACHE_FORUM_TTL) || 5 * 60 * 1000, // 5分钟
} as const;

// 分页配置
export const PAGINATION_CONFIG = {
  DEFAULT_PAGE_SIZE: Number(process.env.REACT_APP_DEFAULT_PAGE_SIZE) || 20,
  MAX_PAGE_SIZE: Number(process.env.REACT_APP_MAX_PAGE_SIZE) || 100,
  PAGE_SIZE_OPTIONS: ['10', '20', '50', '100'],
} as const;

// 验证配置
export const VALIDATION_CONFIG = {
  USERNAME_MIN_LENGTH: 3,
  USERNAME_MAX_LENGTH: 20,
  PASSWORD_MIN_LENGTH: 6,
  PASSWORD_MAX_LENGTH: 128,
  TITLE_MIN_LENGTH: 5,
  TITLE_MAX_LENGTH: 200,
  DESCRIPTION_MAX_LENGTH: 2000,
  CONTENT_MAX_LENGTH: 5000,
} as const;

// 获取完整的资源URL
export const getResourceUrl = (path: string, type: 'avatar' | 'static' | 'cdn' = 'static'): string => {
  if (!path) return '';
  
  // 如果已经是完整URL，直接返回
  if (path.startsWith('http://') || path.startsWith('https://')) {
    return path;
  }
  
  const baseUrl = type === 'avatar' 
    ? RESOURCE_CONFIG.AVATAR_BASE_URL
    : type === 'cdn' 
    ? RESOURCE_CONFIG.CDN_BASE_URL || RESOURCE_CONFIG.STATIC_BASE_URL
    : RESOURCE_CONFIG.STATIC_BASE_URL;
  
  return `${baseUrl}${path.startsWith('/') ? path : `/${path}`}`;
};

// 检查功能是否启用
export const isFeatureEnabled = (feature: keyof typeof FEATURE_FLAGS): boolean => {
  return FEATURE_FLAGS[feature];
};

// 获取环境信息
export const getEnvironmentInfo = () => ({
  name: APP_CONFIG.NAME,
  version: APP_CONFIG.VERSION,
  environment: APP_CONFIG.ENVIRONMENT,
  debug: APP_CONFIG.DEBUG,
  buildTime: process.env.REACT_APP_BUILD_TIME || new Date().toISOString(),
});

// 开发环境日志
export const devLog = (...args: any[]) => {
  if (APP_CONFIG.DEBUG) {
    console.log('[DEV]', ...args);
  }
};

export const devWarn = (...args: any[]) => {
  if (APP_CONFIG.DEBUG) {
    console.warn('[DEV]', ...args);
  }
};

export const devError = (...args: any[]) => {
  if (APP_CONFIG.DEBUG) {
    console.error('[DEV]', ...args);
  }
};
